import pandas as pd
import yaml
from pymongo import MongoClient


def load_config():
    try:
        with open("config.yaml", 'r', encoding='utf-8') as f:
            data = yaml.load(f.read(), yaml.FullLoader)
        print("loading_config ... >>> 读取本地配置")
    except:
        conn = MongoClient("mongodb://hf_user:290202@192.168.3.179:27017/?authSource=admin")
        coll = conn['project_config']['commodity']
        data = list(coll.find())[-1]
        print("loading_config ... >>> 读取服务器配置")
    return data


def loc_zq_collection(db_name, collection_name):
    conn = MongoClient("mongodb://192.168.1.9:27017")
    coll_ = conn[db_name][collection_name]
    return coll_


def loc_collection(db_name, collection_name, cfg={}):
    if len(cfg) == 0:
        cfg = load_config()
    conn = MongoClient(cfg['mongodb'])
    coll_ = conn[db_name][collection_name]
    return coll_


cfg = load_config()
coll_ = loc_collection('to_it', '2022-01-17')

out_dic = {}
for mode in ['期货', '现货', '基差', '价差', '库存']:
    dic = {'pred': True, 'freq': '周',
           'mode': mode}
    data = list(coll_.find(dic))
    out_dic[mode] = data
    print(mode, len(data))
